# ----------------------------=== General ===--------------------------
set -g default-terminal "screen-256color"
if 'infocmp -x tmux-256color > /dev/null 2>&1' 'set -g default-terminal "tmux-256color"'
# true color support
set -ga terminal-overrides ",*256color:RGB"
# undercurl support
set -ga terminal-overrides ',*:Smulx=\E[4::%p1%dm'
# underscore colours support
set -ga terminal-overrides ',*:Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m'
# cursor style support
set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[1 q'

set -g extended-keys always  # enable extended keys in escape sequence, for example, ctrl+shift+letter
set -g escape-time 10
set -g focus-events on
set -g mouse on
set -g set-clipboard on
set -g mode-keys vi

# ----------------------------=== Display ===--------------------------
set -g set-titles off         # set terminal title
set -g base-index 1           # start windows numbering at 1
set -g pane-base-index 1      # make pane numbering consistent with windows
set -g automatic-rename on    # rename window to reflect current program
set -g renumber-windows on    # renumber windows when a window is closed
set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000      # slightly longer status messages display time
set -g monitor-activity on    # monitor for activity in the window.
set -g visual-activity off    # don't display a message instead of sending a bell when activity occurs in monitored window
set -g status-interval 10     # redraw status line every 10 seconds

# ----------------------------=== Theme ===--------------------------
# colors
thm_bg="#1e1e28"
thm_fg="#dadae8"
thm_cyan="#c2e7f0"
thm_black="#15121c"
thm_gray="#332e41"
thm_magenta="#c6aae8"
thm_pink="#e5b4e2"
thm_red="#e38c8f"
thm_green="#b1e3ad"
thm_yellow="#ebdd98"
thm_blue="#a4b9ef"
thm_orange="#ffba96"
thm_black4="#474258"

# status
set -g status on
set -g status-bg "${thm_bg}"
set -g status-position top
set -g status-justify "centre"
set -g status-left-length "100"
set -g status-right-length "100"

# messages
set -g message-style fg="${thm_cyan}",bg="${thm_gray}",align="centre"
set -g message-command-style fg="${thm_cyan}",bg="${thm_gray}",align="centre"

# panes
set -g pane-border-style fg="${thm_gray}"
set -g pane-active-border-style fg="${thm_blue}"

# windows
setw -g window-status-activity-style fg="${thm_fg}",bg="${thm_bg}",none
setw -g window-status-separator ""
setw -g window-status-style fg="${thm_fg}",bg="${thm_bg}",none

# statusline
setw -g window-status-format "#[fg=$thm_blue,bg=$thm_bg]#[fg=$thm_bg,bg=$thm_blue,italics]#I #[fg=$thm_fg,bg=$thm_gray] #W#[fg=$thm_gray,bg=$thm_bg] "
setw -g window-status-current-format "#{?client_prefix,#[fg=$thm_red],#{?window_zoomed_flag,#[fg=$thm_yellow],#[fg=$thm_orange]}}#[bg=$thm_bg]#[fg=$thm_bg,italics]#{?client_prefix,#[bg=$thm_red],#{?window_zoomed_flag,#[bg=$thm_yellow],#[bg=$thm_orange]}}#I #{?client_prefix,#[fg=$thm_red],#{?window_zoomed_flag,#[fg=$thm_yellow],#[fg=$thm_fg]}}#[bg=$thm_black4] #W#[fg=$thm_black4,bg=$thm_bg] "
set -g status-left "#[fg=$thm_pink,bg=$thm_bg,nobold,nounderscore,noitalics]#[fg=$thm_bg,bg=$thm_pink,nobold,nounderscore,noitalics] #S #[fg=$thm_pink,bg=$thm_bg,nobold,nounderscore,noitalics] #[fg=$thm_green,bg=$thm_gray]#[bg=$thm_green,fg=$thm_bg]ﱿ #{b:pane_current_path} #[fg=$thm_green,bg=$thm_bg]"
set -g status-right "#[fg=$thm_cyan,bg=$thm_bg,nobold,nounderscore,noitalics]#[fg=$thm_bg,bg=$thm_cyan,nobold,nounderscore,noitalics] %H:%M #[fg=$thm_cyan,bg=$thm_bg,nobold,nounderscore,noitalics] #[fg=$thm_magenta,bg=$thm_bg,nobold,nounderscore,noitalics]#[fg=$thm_bg,bg=$thm_magenta,nobold,nounderscore,noitalics] %F #[fg=$thm_magenta,bg=$thm_bg,nobold,nounderscore,noitalics]"

# modes
setw -g clock-mode-colour "${thm_blue}"
setw -g mode-style "fg=${thm_pink} bg=${thm_black4} bold"

# ----------------------------=== Keybindings ===--------------------------
# prefix
unbind C-Z
unbind C-B
set -g prefix M-w

# clear both screen and history
bind -n C-l send-keys C-l \; run 'sleep 0.2' \; clear-history

# reload config file
bind R source-file ~/.tmux.conf \; display-message "Config reloaded.."

# pane operator
bind s splitw -v -c '#{pane_current_path}'
bind v splitw -h -c '#{pane_current_path}'
bind h selectp -L
bind j selectp -D
bind k selectp -U
bind l selectp -R
bind + resizep -U 10
bind - resizep -D 10
bind < resizep -L 10
bind > resizep -R 10

# clipboard
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection
bind ] run-shell -b "win32yank.exe -o --lf | tmux load-buffer - ; tmux paste-buffer"

# fast launch
bind t new-window htop
bind T new-window btop
bind g new-window -c "#{pane_current_path}" tig --all
bind r new-window -c "#{pane_current_path}" ranger
bind m new-window "cmatrix"

# mouse wheel simulation
tmux_commands_with_legacy_scroll="nano less more man"
bind-key -T root WheelUpPane \
    if-shell -Ft= '#{?mouse_any_flag,1,#{pane_in_mode}}' \
        'send -Mt=' \
        'if-shell -t= "#{?alternate_on,true,false} || echo \"#{tmux_commands_with_legacy_scroll}\" | grep -q \"#{pane_current_command}\"" \
            "send -t= Up Up Up" "copy-mode -et="'
bind-key -T root WheelDownPane \
    if-shell -Ft = '#{?pane_in_mode,1,#{mouse_any_flag}}' \
        'send -Mt=' \
        'if-shell -t= "#{?alternate_on,true,false} || echo \"#{tmux_commands_with_legacy_scroll}\" | grep -q \"#{pane_current_command}\"" \
            "send -t= Down Down Down" "send -Mt="'

# ----------------------------=== Plugins ===--------------------------
run '/usr/share/tmux-plugin-manager/tpm'        # plugin manager
set -g @plugin 'tmux-plugins/tmux-resurrect'    # store and restore session

# ----------------------------=== Env ===--------------------------
EDITOR=nvim
PATH=$HOME/.local/bin:$HOME/.cargo/bin:$PATH
